<template>
  <div class="login">
    <div class="main">
      <h2>Login</h2>
      
         <el-form
      :model="ruleForm"
      status-icon
      :rules="rules"
      ref="ruleForm"
      label-width="100px"
      class="demo-ruleForm"
      label-position='top'
    >
      <el-form-item label="用户名" prop="name">
        <el-input
          v-model="ruleForm.name"
          autocomplete="off"
        ></el-input>
      </el-form-item>
      <el-form-item label="密码" prop="passWord">
        <el-input
          type="password"
          v-model="ruleForm.passWord"
          autocomplete="off"
          @keyup.enter.native="doLogin"
        ></el-input>
      </el-form-item>
      <!-- @click="submitForm('ruleForm')" -->
      <el-form-item>
        <!-- <el-button type="primary" @click="submitForm('ruleForm')"
          >提交</el-button
        > -->
        <el-button @click="doLogin()">登录 </el-button>
        <el-button @click="resetForm('ruleForm')">清空</el-button>
      </el-form-item>
    </el-form>
      
     
    </div>
    
  </div>
</template>
<script>
import store from "@/store";
import {SET_ITEM,SET_USERINFO} from "@/store/mutation_types";
import {mapState,mapMutations} from "vuex"
export default {
  computed:{
    ...mapState(['token'])
  },
  data() {
    let validatename = (rule, value, callback) => {
      if (value === "") {
        callback(new Error("请输入用户名"));
      } else {
        callback();
      }
    };
    let validatepass = (rule, value, callback) => {
      if (value === "") {
        callback(new Error("请输入密码"));
      } else {
        callback();
      }
    };
    return {
      ruleForm: {
        name: "",
        passWord: "",
      },
      rules: {
        name: [{ validator: validatename, trigger: "blur" }],
        passWord: [{ validator: validatepass, trigger: "blur" }],
      },
    };
  },
  methods: {
    ...mapMutations(['SET_ITEM']),
    submitForm(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          console.log(valid,'valid')
          // this.doLogin()
        } else {
          console.log("error submit!!");
          return false;
        }
      });
    },
    resetForm(formName) {
      this.$refs[formName].resetFields();
    },
    doLogin(){

      if(!this.ruleForm.name){
        this.$message({
          type:'warning',
          message:'User name is required'
        })
        return false
      }
      if(!this.ruleForm.passWord){
        this.$message({
          type:'warning',
          message:'Password is required'
        })
        return false
      }
      this.$api.default.doLogin({
        userId:this.ruleForm.name,
        password:this.ruleForm.passWord
      }).then((res) => {
        store.commit(SET_USERINFO, res.data);//未用到
        sessionStorage.setItem('token',res.data.token)
        sessionStorage.setItem('userId',res.data.userId)
        this.$router.push('/')
      });
    }
  },
};
</script>
<style lang='scss' scoped>
  .login{
    min-height: 100vh;
    background: linear-gradient(270deg, #8853ff 0%, #5564fe 100%);
    display: flex;
    justify-content: center;
    align-items: center;
    .main{
      width: 400px;
      height: auto;
      background: #fff;
      border-radius: 5px;
      margin: 0 auto;
      border: solid 1px #ddd;
      box-shadow: 0 0 5px rgba($color: #000000, $alpha: 0.7);
      h2{
        text-align: center;
      }
      
      ::v-deep{
        .el-form-item__label{
          padding: 0;
        }
        .el-form{
              padding:0 25px;
        }
      }
      
    }
  }
  @media screen and (max-width: 768px)  {
    .login{
      .main{
        width: 100vw;
        .from_box{
          width: 85vw;
        }
      }
    }
  }
</style>
